/*
 * HttpFileUploader.java
 * 
 * This class runs in its own thread, but does not initiate
 * until the 'upload' button has been clicked (in the UrbanRemix class).
 * 
 * Once initiated, it will sleep for 5 seconds, then attempt to 
 * upload all files in the 'queue' folder: WAV's followed by JPG's.
 * 
 * Sounds are uploaded first. Because they are used to identify the 
 * JPG's, they must be present in the database before any
 * associated JPG's are uploaded. If a JPG is uploaded first,
 * we won't be able to identify the foreign key of its 
 * WAV file, and the upload will fail. See mupload.php
 * and database.php for more information.
 * 
 * The uploadAllQueuedFiles() function uses a file filter 
 * to run the uploadSound() function on each WAV file.
 * Then it runs uploadImage() on the remaining JPG
 * files.
 *
 * The respective upload() functions write the HTTP
 * headers via POST, which is received by the mupload.php
 * function.
 * 
 * Upon successfully completing an upload, this class
 * will delete the uploaded file from the queue folder.
 * 
 * 
 */

package com.android.UrbanRemix;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.android.UrbanRemix.UrbanRemix_New;
import com.android.UrbanRemix.WavIO;

import android.content.Context;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

/**
 * this class allows for standard http file uploads variables are passed to a
 * receiving php page
 */

public class HttpFileUploader extends Thread {
	public Handler mHandler;
	URL connectURL;
	UploadFunctionality urbanremix;

	private boolean isUploading;
	private boolean paused;

	byte[] dataToServer;

	// ------
	// The constructor simply defines the URL for
	// uploads (mupload.php), and doesn't initiate
	// the thread; that's handled instead by the
	// completion of the TagSelection class, which
	// triggers the onActivityResult() function
	// in the UrbanRemix class.

	HttpFileUploader(UploadFunctionality urbanremix, String urlString) {
		super();
		this.urbanremix = urbanremix;
		this.isUploading = false;

		try {
			connectURL = new URL(urlString);
		} catch (Exception ex) {

		}

	}

	private String uploadAll(FileInputStream myFileInputStream, File file,
			String latitude, String longitude, String accuracy,
			String file_time, String file_user, String file_password,
			String tags, String proper_time) {

		String lineEnd = "\r\n"; // newline
		String twoHyphens = "--"; // prefix
		String boundary = "*****";
		String Tag = "3rd";
		String absFileName = file.getPath();
		String fileName = absFileName.substring(
				absFileName.lastIndexOf("/") + 1, absFileName.length());

		try {
			// ------------------ CLIENT REQUEST

			Log.e(Tag, "Starting to bad things");
			// Open a HTTP connection to the URL

			HttpURLConnection conn = (HttpURLConnection) connectURL
					.openConnection();

			// Allow Inputs
			conn.setDoInput(true);

			// Allow Outputs
			conn.setDoOutput(true);

			// Don't use a cached copy.
			conn.setUseCaches(false);

			// Use a post method.
			conn.setRequestMethod("POST");

			conn.setRequestProperty("Connection", "Keep-Alive");

			conn.setRequestProperty("Content-Type",
					"multipart/form-data;boundary=" + boundary);

			DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

			// send latitude data
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos.writeBytes("Content-Disposition: form-data; name=\"latitude\"");
			dos.writeBytes(lineEnd + lineEnd);
			dos.writeBytes(latitude);
			dos.writeBytes(lineEnd);
			Log.e(Tag, "latitude: " + latitude);

			// send longitude data
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos
					.writeBytes("Content-Disposition: form-data; name=\"longitude\"");
			dos.writeBytes(lineEnd + lineEnd);
			dos.writeBytes(longitude);
			dos.writeBytes(lineEnd);
			Log.e(Tag, "longitude: " + longitude);

			// send accuracy data
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos.writeBytes("Content-Disposition: form-data; name=\"accuracy\"");
			dos.writeBytes(lineEnd + lineEnd);
			dos.writeBytes(accuracy);
			dos.writeBytes(lineEnd);
			Log.e(Tag, "accuracy: " + accuracy);

			// send time data
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos.writeBytes("Content-Disposition: form-data; name=\"time\"");
			dos.writeBytes(lineEnd + lineEnd);
			dos.writeBytes(file_time);
			dos.writeBytes(lineEnd);
			Log.e(Tag, "time: " + file_time);

			// send time data
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos.writeBytes("Content-Disposition: form-data; name=\"time\"");
			dos.writeBytes(lineEnd + lineEnd);
			dos.writeBytes(file_time);
			dos.writeBytes(lineEnd);
			Log.e(Tag, "ptime: " + proper_time);

			// send user name data
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos.writeBytes("Content-Disposition: form-data; name=\"user\"");
			dos.writeBytes(lineEnd + lineEnd);
			dos.writeBytes(file_user);
			dos.writeBytes(lineEnd);
			Log.e(Tag, "user: " + file_user);

			// send user password
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos.writeBytes("Content-Disposition: form-data; name=\"password\"");
			dos.writeBytes(lineEnd + lineEnd);
			dos.writeBytes(file_password);
			dos.writeBytes(lineEnd);
			Log.e(Tag, "password sent");

			// send tags
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos.writeBytes("Content-Disposition: form-data; name=\"tags\"");
			dos.writeBytes(lineEnd + lineEnd);
			dos.writeBytes(tags);
			dos.writeBytes(lineEnd);
			Log.e(Tag, "tags: " + tags);

			// start the file
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos
					.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
							+ fileName + "\"" + lineEnd);
			dos.writeBytes(lineEnd);

			Log.e(Tag, "Headers are written");

			// create a buffer of maximum size

			int bytesAvailable = myFileInputStream.available();
			int maxBufferSize = 1024;
			int bufferSize = Math.min(bytesAvailable, maxBufferSize);
			byte[] buffer = new byte[bufferSize];

			// read file and write it into form...

			int bytesRead = myFileInputStream.read(buffer, 0, bufferSize);
			Log.e(Tag, "bytesAvailable: " + bytesAvailable);

			while (bytesRead > 0) {
				dos.write(buffer, 0, bufferSize);
				bytesAvailable = myFileInputStream.available();
				bufferSize = Math.min(bytesAvailable, maxBufferSize);
				bytesRead = myFileInputStream.read(buffer, 0, bufferSize);
			}

			// send multipart form data necessary after file data...

			dos.writeBytes(lineEnd);
			dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

			// close streams
			Log.e(Tag, "File is written");
			myFileInputStream.close();
			dos.flush();

			InputStream is = conn.getInputStream();

			// retrieve the response from server
			int ch;

			StringBuffer b = new StringBuffer();

			while ((ch = is.read()) != -1) {
				b.append((char) ch);
			}

			String s = b.toString();
			Log.i("Response", s);

			dos.close();
			// sendMessage("successful upload of "+fileName);

			return new String(s);

		} catch (MalformedURLException ex) {
			Log.e(Tag, "error: " + ex.getMessage(), ex);
			sendMessage("Upload failed due to malformed URL.");

			return new String("MalformedURLException");
		} catch (IOException ioe) {
			Log.e(Tag, "error: " + ioe.getMessage(), ioe);
			sendMessage("Upload failed due to IO Error.");
			return new String("IOException");
		}

	}

	// ----------
	// run() not called until the completion of TagSelection.
	// sleeps for 5 seconds, then uploads all queued files.

	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(5000);
				uploadAllQueuedFiles();
			} catch (Exception e) {
			}
		}
	}

	// -----------
	// This private class is used to
	// filter by filename, allowing us to
	// upload WAV files before JPG's.

	private class MyFileFilter implements FileFilter {
		String ext;

		MyFileFilter(String ext) {
			this.ext = ext;
		}

		public boolean accept(File f) {
			String name = f.getName().toLowerCase();
			if (name.endsWith(ext))
				return true;
			else
				return false;
		}
	}

	// ---------
	// Upload all files in the 'queue' folder starting with WAV's,
	// so the database entry for each WAV file exists for the JPG's
	// which will refer to them.

	private void uploadAllQueuedFiles() {

		File queueFolder = urbanremix.getQueueFolder();

		File[] queuedFiles = queueFolder.listFiles();
		if (queuedFiles.length > 0) {
			for (int i = 0; i < queuedFiles.length; i++) {

				if (paused) {
					sendMessage("Result: in if");
					break;
				} else {

					String absFileName = queuedFiles[i].getPath();

					String filename = absFileName
							.substring(absFileName.lastIndexOf("/") + 1,
									absFileName.lastIndexOf("."));

					String ext = absFileName.substring(absFileName
							.lastIndexOf(".") + 1, absFileName.length());

					String time_extract = filename.substring(filename
							.indexOf("-") + 1, filename.length());
					// sendMessage("Result: "+time_extract);
					String time_splits[] = time_extract.split("-");
					String time = time_splits[0] + "-" + time_splits[1] + "-"
							+ time_splits[2] + " " + time_splits[3] + ":"
							+ time_splits[4] + ":" + time_splits[5];
					String username = urbanremix.getUsername();
					String password = urbanremix.getPassword();

					String lat = urbanremix.getFileLatitude(filename);
					String lng = urbanremix.getFileLongitude(filename);
					String acc = urbanremix.getFileAccuracy(filename);
					String tags = urbanremix.getFileTags(filename);

					try {
						FileInputStream fis = new FileInputStream(
								queuedFiles[i]);
						sendMessage("Uploading file "
								+ queuedFiles[i].getPath() + "... (size: "
								+ (queuedFiles[i].length() / 1000) + "kb)"
								+ "lat" + lat);
						String result = uploadAll(fis, queuedFiles[i], lat,
								lng, acc, time, username, password, tags, time);
						if (result.indexOf("was uploaded") != -1)
							queuedFiles[i].delete();
						sendMessage("Result: " + result);

						urbanremix.unregisterFilePrefs(filename);
					} catch (FileNotFoundException e) {
					}

				}
			}
		}
	}

	// -------
	// Used to pass a message to the GUI, which appears in the status bar
	// text field.

	private void sendMessage(String messageContents) {
		Bundle b = new Bundle();
		b.putString("message", messageContents);
		Message m = new Message();
		m.setData(b);
		urbanremix.getHandler().sendMessage(m);
	}

	// -----------
	// mutators & accessors

	// @param paused
	public void setPaused(boolean b) {
		this.paused = b;
	}

	// @return paused
	public boolean getPaused() {
		return this.paused;
	}

	// @param isUploading
	public void setUploading(boolean b) {
		this.isUploading = b;
	}

	// @return isUploading
	public boolean isUploading() {
		return this.isUploading;
	}

}

// ----------
// Upload a sound; requires these parameters:
// the file itself; GPS data; the file timestamp; the username/password; and the
// comma-separated string of tags.
// the GPS data and tags are obtained from the saved shared preferences.
// the file is obtained from the 'queue' folder.

/*
 * 
 * 
 * 
 * private String uploadSound(FileInputStream myFileInputStream, File file,
 * String latitude, String longitude, String accuracy, String file_time, String
 * file_user, String file_password, String tags) {
 * 
 * String lineEnd = "\r\n"; //newline String twoHyphens = "--"; //prefix String
 * boundary = "*****"; String Tag="3rd"; String absFileName = file.getPath();
 * String fileName = absFileName.substring(absFileName.lastIndexOf("/")+1,
 * absFileName.length());
 * 
 * try { //------------------ CLIENT REQUEST
 * 
 * Log.e(Tag,"Starting to bad things"); // Open a HTTP connection to the URL
 * 
 * HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
 * 
 * // Allow Inputs conn.setDoInput(true);
 * 
 * // Allow Outputs conn.setDoOutput(true);
 * 
 * // Don't use a cached copy. conn.setUseCaches(false);
 * 
 * // Use a post method. conn.setRequestMethod("POST");
 * 
 * conn.setRequestProperty("Connection", "Keep-Alive");
 * 
 * conn.setRequestProperty("Content-Type",
 * "multipart/form-data;boundary="+boundary);
 * 
 * DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );
 * 
 * //send latitude data dos.writeBytes(twoHyphens + boundary + lineEnd);
 * dos.writeBytes("Content-Disposition: form-data; name=\"latitude\"");
 * dos.writeBytes(lineEnd+lineEnd); dos.writeBytes(latitude);
 * dos.writeBytes(lineEnd); Log.e(Tag,"latitude: "+latitude);
 * 
 * //send longitude data dos.writeBytes(twoHyphens + boundary + lineEnd);
 * dos.writeBytes("Content-Disposition: form-data; name=\"longitude\"");
 * dos.writeBytes(lineEnd+lineEnd); dos.writeBytes(longitude);
 * dos.writeBytes(lineEnd); Log.e(Tag,"longitude: "+longitude);
 * 
 * //send accuracy data dos.writeBytes(twoHyphens + boundary + lineEnd);
 * dos.writeBytes("Content-Disposition: form-data; name=\"accuracy\"");
 * dos.writeBytes(lineEnd+lineEnd); dos.writeBytes(accuracy);
 * dos.writeBytes(lineEnd); Log.e(Tag,"accuracy: "+accuracy);
 * 
 * //send time data dos.writeBytes(twoHyphens + boundary + lineEnd);
 * dos.writeBytes("Content-Disposition: form-data; name=\"time\"");
 * dos.writeBytes(lineEnd+lineEnd); dos.writeBytes(file_time);
 * dos.writeBytes(lineEnd); Log.e(Tag,"time: "+file_time);
 * 
 * //send user name data dos.writeBytes(twoHyphens + boundary + lineEnd);
 * dos.writeBytes("Content-Disposition: form-data; name=\"user\"");
 * dos.writeBytes(lineEnd+lineEnd); dos.writeBytes(file_user);
 * dos.writeBytes(lineEnd); Log.e(Tag,"user: "+file_user);
 * 
 * //send user password dos.writeBytes(twoHyphens + boundary + lineEnd);
 * dos.writeBytes("Content-Disposition: form-data; name=\"password\"");
 * dos.writeBytes(lineEnd+lineEnd); dos.writeBytes(file_password);
 * dos.writeBytes(lineEnd); Log.e(Tag,"password sent");
 * 
 * //send tags dos.writeBytes(twoHyphens + boundary + lineEnd);
 * dos.writeBytes("Content-Disposition: form-data; name=\"tags\"");
 * dos.writeBytes(lineEnd+lineEnd); dos.writeBytes(tags);
 * dos.writeBytes(lineEnd); Log.e(Tag,"tags: "+tags);
 * 
 * //start the file dos.writeBytes(twoHyphens + boundary + lineEnd);
 * dos.writeBytes
 * ("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" +
 * fileName +"\"" + lineEnd); dos.writeBytes(lineEnd);
 * 
 * Log.e(Tag,"Headers are written");
 * 
 * // create a buffer of maximum size
 * 
 * int bytesAvailable = myFileInputStream.available(); int maxBufferSize = 1024;
 * int bufferSize = Math.min(bytesAvailable, maxBufferSize); byte[] buffer = new
 * byte[bufferSize];
 * 
 * // read file and write it into form...
 * 
 * int bytesRead = myFileInputStream.read(buffer, 0, bufferSize);
 * Log.e(Tag,"bytesAvailable: "+bytesAvailable);
 * 
 * while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable =
 * myFileInputStream.available(); bufferSize = Math.min(bytesAvailable,
 * maxBufferSize); bytesRead = myFileInputStream.read(buffer, 0, bufferSize); }
 * 
 * // send multipart form data necessary after file data...
 * 
 * dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens +
 * lineEnd);
 * 
 * // close streams Log.e(Tag,"File is written"); myFileInputStream.close();
 * dos.flush();
 * 
 * InputStream is = conn.getInputStream();
 * 
 * // retrieve the response from server int ch;
 * 
 * StringBuffer b = new StringBuffer();
 * 
 * while( ( ch = is.read() ) != -1 ) { b.append( (char)ch ); }
 * 
 * String s = b.toString(); Log.i("Response",s);
 * 
 * dos.close(); //sendMessage("successful upload of "+fileName);
 * 
 * return new String(s);
 * 
 * } catch (MalformedURLException ex) { Log.e(Tag, "error: " + ex.getMessage(),
 * ex); sendMessage("Upload failed due to malformed URL.");
 * 
 * return new String("MalformedURLException"); } catch (IOException ioe) {
 * Log.e(Tag, "error: " + ioe.getMessage(), ioe);
 * sendMessage("Upload failed due to IO Error."); return new
 * String("IOException"); }
 * 
 * 
 * }
 */

// --------
// Upload an image; requires these parameters:
// the file and timestamp; the username/password; and the associated WAV file.
// the username/password are obtained from UrbanRemix class.
// the WAV filename is acquired from the saved shared preferences for this
// particular JPG file.

/*
 * 
 * private String uploadImage(FileInputStream myFileInputStream, File file,
 * String file_time, String file_user, String file_password, String wavfilename)
 * {
 * 
 * String lineEnd = "\r\n"; //newline String twoHyphens = "--"; //prefix String
 * boundary = "*****"; String Tag="3rd"; String absFileName = file.getPath();
 * //String fileName = absFileName.substring(absFileName.lastIndexOf("/")+1,
 * absFileName.lastIndexOf("."));
 * 
 * String fileName = absFileName.substring(absFileName.lastIndexOf("/")+1,
 * absFileName.length());
 * 
 * try { //------------------ CLIENT REQUEST
 * 
 * Log.e(Tag,"Starting to bad things"); // Open a HTTP connection to the URL
 * 
 * HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
 * 
 * // Allow Inputs conn.setDoInput(true);
 * 
 * // Allow Outputs conn.setDoOutput(true);
 * 
 * // Don't use a cached copy. conn.setUseCaches(false);
 * 
 * // Use a post method. conn.setRequestMethod("POST");
 * 
 * conn.setRequestProperty("Connection", "Keep-Alive");
 * 
 * conn.setRequestProperty("Content-Type",
 * "multipart/form-data;boundary="+boundary);
 * 
 * DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );
 * 
 * //send time data dos.writeBytes(twoHyphens + boundary + lineEnd);
 * dos.writeBytes("Content-Disposition: form-data; name=\"time\"");
 * dos.writeBytes(lineEnd+lineEnd); dos.writeBytes(file_time);
 * dos.writeBytes(lineEnd); Log.e(Tag,"time: "+file_time);
 * 
 * //send time data dos.writeBytes(twoHyphens + boundary + lineEnd);
 * dos.writeBytes("Content-Disposition: form-data; name=\"wavfilename\"");
 * dos.writeBytes(lineEnd+lineEnd); dos.writeBytes(wavfilename);
 * dos.writeBytes(lineEnd); Log.e(Tag,"time: "+wavfilename);
 * 
 * //send user name data dos.writeBytes(twoHyphens + boundary + lineEnd);
 * dos.writeBytes("Content-Disposition: form-data; name=\"user\"");
 * dos.writeBytes(lineEnd+lineEnd); dos.writeBytes(file_user);
 * dos.writeBytes(lineEnd); Log.e(Tag,"user: "+file_user);
 * 
 * //send user password dos.writeBytes(twoHyphens + boundary + lineEnd);
 * dos.writeBytes("Content-Disposition: form-data; name=\"password\"");
 * dos.writeBytes(lineEnd+lineEnd); dos.writeBytes(file_password);
 * dos.writeBytes(lineEnd); Log.e(Tag,"password sent");
 * 
 * //start the file dos.writeBytes(twoHyphens + boundary + lineEnd);
 * dos.writeBytes
 * ("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" +
 * fileName +"\"" + lineEnd); dos.writeBytes(lineEnd);
 * 
 * Log.e(Tag,"Headers are written");
 * 
 * // create a buffer of maximum size
 * 
 * int bytesAvailable = myFileInputStream.available(); int maxBufferSize = 1024;
 * int bufferSize = Math.min(bytesAvailable, maxBufferSize); byte[] buffer = new
 * byte[bufferSize];
 * 
 * // read file and write it into form...
 * 
 * int bytesRead = myFileInputStream.read(buffer, 0, bufferSize);
 * Log.e(Tag,"bytesAvailable: "+bytesAvailable);
 * 
 * while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable =
 * myFileInputStream.available(); bufferSize = Math.min(bytesAvailable,
 * maxBufferSize); bytesRead = myFileInputStream.read(buffer, 0, bufferSize); }
 * 
 * // send multipart form data necessary after file data...
 * 
 * dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens +
 * lineEnd);
 * 
 * // close streams Log.e(Tag,"File is written"); myFileInputStream.close();
 * dos.flush();
 * 
 * InputStream is = conn.getInputStream();
 * 
 * // retrieve the response from server int ch;
 * 
 * StringBuffer b = new StringBuffer();
 * 
 * while( ( ch = is.read() ) != -1 ) { b.append( (char)ch ); }
 * 
 * String s = b.toString(); Log.i("Response",s);
 * 
 * dos.close(); //sendMessage("successful upload of "+fileName);
 * 
 * return new String(s);
 * 
 * } catch (MalformedURLException ex) { Log.e(Tag, "error: " + ex.getMessage(),
 * ex); sendMessage("Upload failed due to malformed URL.");
 * 
 * return new String("MalformedURLException"); } catch (IOException ioe) {
 * Log.e(Tag, "error: " + ioe.getMessage(), ioe);
 * sendMessage("Upload failed due to IO Error."); return new
 * String("IOException"); } }
 */

/*
 * private void uploadAllQueuedFiles() { WavIO wavio = urbanremix.wavio; File
 * queueFolder = wavio.getQueueFolder();
 * 
 * File [] queuedWavFiles = queueFolder.listFiles(new MyFileFilter("wav")); if
 * (queuedWavFiles.length > 0) { for (int i = 0; i < queuedWavFiles.length; i++)
 * { if (paused) break; else { String absFileName = queuedWavFiles[i].getPath();
 * String filename = absFileName.substring(absFileName.lastIndexOf("/")+1,
 * absFileName.lastIndexOf(".")); String ext =
 * absFileName.substring(absFileName.lastIndexOf(".")+1, absFileName.length());
 * 
 * String time_extract = filename.substring(0, filename.lastIndexOf("-"));
 * String time_splits[] = time_extract.split("-"); String time =
 * time_splits[0]+"-"+time_splits[1]+"-"+time_splits[2]+ " " + time_splits[3]+
 * ":" + time_splits[4] + ":" + time_splits[5] ; String username =
 * urbanremix.getUsername(); String password = urbanremix.getPassword();
 * 
 * if (ext.equals(new String("wav"))) { String lat =
 * urbanremix.getFileLatitude(filename); String lng =
 * urbanremix.getFileLongitude(filename); String acc =
 * urbanremix.getFileAccuracy(filename); String tags =
 * urbanremix.getFileTags(filename);
 * 
 * try { FileInputStream fis = new FileInputStream(queuedWavFiles[i]);
 * sendMessage
 * ("Uploading file "+queuedWavFiles[i].getPath()+"... (size: "+(queuedWavFiles
 * [i].length()/1000)+"kb)");
 * 
 * String result = uploadSound(fis, queuedWavFiles[i], lat, lng, acc, time,
 * username, password, tags); if (result.indexOf("was uploaded") != -1)
 * queuedWavFiles[i].delete(); sendMessage("Result: "+result);
 * 
 * urbanremix.unregisterFilePrefs(filename); } catch (FileNotFoundException e)
 * {}
 * 
 * try { Thread.sleep(5000); } catch (Exception e) {
 * 
 * } } } } }
 * 
 * File[] queuedJpegFiles; queuedJpegFiles = queueFolder.listFiles(new
 * MyFileFilter("jpg"));
 * 
 * for (int i = 0; i < queuedJpegFiles.length; i++) { if (paused) break; else {
 * String absFileName = queuedJpegFiles[i].getPath(); String filename =
 * absFileName.substring(absFileName.lastIndexOf("/")+1,
 * absFileName.lastIndexOf(".")); String ext =
 * absFileName.substring(absFileName.lastIndexOf(".")+1, absFileName.length());
 * 
 * String time_extract = filename.substring(0, filename.lastIndexOf("-"));
 * String time_splits[] = time_extract.split("-"); String time =
 * time_splits[0]+"-"+time_splits[1]+"-"+time_splits[2]+ " " + time_splits[3]+
 * ":" + time_splits[4] + ":" + time_splits[5] ; String username =
 * urbanremix.getUsername(); String password = urbanremix.getPassword();
 * 
 * if (ext.equals(new String("jpg"))) {
 * 
 * String jpgfilename = absFileName.substring(absFileName.lastIndexOf("/")+1,
 * absFileName.lastIndexOf("."));
 * 
 * int mode = Activity.MODE_PRIVATE; SharedPreferences mySharedPreferences =
 * urbanremix.getSharedPreferences(urbanremix.MYPREFS, mode); String
 * associatedWav = mySharedPreferences.getString(jpgfilename+"Wav", "");
 * 
 * //uploadImage(
 * sendMessage(jpgfilename+" - associated wav for "+jpgfilename+"Wav: "
 * +associatedWav);
 * 
 * try { FileInputStream fis = new FileInputStream(queuedJpegFiles[i]);
 * sendMessage
 * ("Uploading file "+queuedJpegFiles[i].getPath()+"... (size: "+(queuedJpegFiles
 * [i].length()/1000)+"kb)"); String result = uploadImage(fis,
 * queuedJpegFiles[i], time, username, password, associatedWav); if
 * (result.indexOf("was uploaded") != -1) queuedJpegFiles[i].delete();
 * sendMessage("Result: "+result); SharedPreferences.Editor editor =
 * mySharedPreferences.edit(); editor.remove(jpgfilename+"Wav");
 * editor.commit(); } catch (FileNotFoundException e) {}
 * 
 * try { Thread.sleep(5000); } catch (Exception e) {}
 * 
 * } } } }
 */

